package com.wxmit.module.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wxmit.framework.data.permission.core.annotation.DataPermission;
import com.wxmit.module.system.domain.SysMenu;
import com.wxmit.module.system.domain.dto.BatchAddBtnDto;
import com.wxmit.module.system.enums.SysMenuTypeEnum;
import com.wxmit.module.system.mapper.SysMenuMapper;
import com.wxmit.module.system.mapper.SysRoleMenuMapper;
import com.wxmit.module.system.service.ISysMenuService;
import com.wxmit.system.base.core.ShushanServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description SysMenuServiceImpl
 * @Author huangxiong
 * @Date 2022/7/12 10:16
 * @Version V1.0
 */
@Service
public class SysMenuServiceImpl extends ShushanServiceImpl<SysMenuMapper, SysMenu> implements ISysMenuService {

    @Autowired
    private SysRoleMenuMapper roleMenuMapper;

    @DataPermission
    @Override
    public List<SysMenu> list() {
        return super.list();
    }

    @Override
    public List<Tree<Long>> listTree(List<Long> roleIds) {
        List<SysMenu> list = baseMapper.listMenuByRoleIds(roleIds);
        return buildMenuTree(list);
    }

    @Override
    public List<Tree<Long>> listTree() {
        List<SysMenu> list = baseMapper.listMenuByRoleIds(null);
        return buildMenuTree(list);
    }

    @Override
    public List<Tree<Long>> listMenuTree() {
        List<SysMenu> list = list();
        return buildMenuTree(list);
    }

    @Override
    public List<Tree<Long>> listRouterTree() {
        List<SysMenu> list = list(new LambdaQueryWrapper<SysMenu>().ne(SysMenu::getType, SysMenuTypeEnum.BUTTON.code));
        return buildMenuTree(list);
    }

    @Override
    public Boolean batchAddBtn(Long pId, List<BatchAddBtnDto> list) {
        List<SysMenu> btnList = list.stream().map(it -> {
            SysMenu btn = new SysMenu();
            btn.setParentId(pId);
            btn.setName(it.getName());
            btn.setMenuKey("/");
            btn.setPath("/");
            btn.setPermission(it.getPermission());
            btn.setType(SysMenuTypeEnum.BUTTON.code);
            return btn;
        }).collect(Collectors.toList());

        baseMapper.insertBatch(btnList);
        return true;
    }

    @Override
    public boolean removeById(Serializable id) {

        Long count = lambdaQuery().eq(SysMenu::getParentId, id).count();
        Assert.isTrue(count.compareTo(0L)==0,"不可删除有子节点的菜单");

        return super.removeById(id);
    }

    public  List<Tree<Long>> buildMenuTree(List<SysMenu> list){
        if(CollectionUtil.isEmpty(list))return Collections.emptyList();
        //配置
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();

        List<Tree<Long>> treeList = TreeUtil.build(list, 0L, treeNodeConfig, (it, tree) -> {
            tree.setId(it.getId()).setName(it.getName()).setWeight(it.getWeight()).setParentId(it.getParentId());
            tree.put("menuKey", it.getMenuKey());
            tree.put("path", it.getPath());
            tree.put("icon", it.getIcon());
            tree.put("type", it.getType());
            tree.put("status", it.getStatus());
            tree.put("hide", it.getHide());
            tree.put("iframe", it.getIframe());
            tree.put("permission", it.getPermission());
            tree.put(SysMenu.Fields.watermark,it.getWatermark());
            tree.put(SysMenu.Fields.keepAlive,it.getKeepAlive());
        });
        return treeList;
    }
}
